package com.moyun.websocket.controller;

import com.moyun.common.utils.MemorySessionUtil;
import com.moyun.model.po.system.SysUserEntity;
import com.moyun.websocket.model.ChatMessage;
import com.moyun.websocket.po.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequiredArgsConstructor
@Api(tags="聊天室")
public class ChatController {

    private final MemorySessionUtil memorySessionUtil;

    /**
     * @SendTo定义了消息的目的地。结合例子解释就是“接收/chat.sendMessage发来的value，然后将value转发到/topic/notice客户端。 /topic/notice是客户端发起连接后，订阅服务端消息时指定的一个地址，用于接收服务端的返回。
     */
    // 客户端推送消息的端点
    @MessageMapping("/chat.sendMessage")
    // 客户端订阅消息的端点
    @SendTo("/topic/pulic")
    public ChatMessage sendMessage(@Payload ChatMessage chatMessage) {
        return chatMessage;
    }

    // 客户端推送消息的端点
    @MessageMapping("/chat.addUser")
    // 客户端订阅消息的端点
    @SendTo("/topic/pulic")
    public ChatMessage addUser(@Payload ChatMessage chatMessage, SimpMessageHeaderAccessor headerAccessor) {
        headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());
        return chatMessage;
    }

    @ApiOperation("进入聊天室页面")
    @RequestMapping("/toChatroom")
    public String loginvalidate() {
        return "chatroom";
    }

    /**
     * @Author yangboxuan
     * @Description 获取当前登录用户的信息
     * @Date 2021/9/5 11:37
     * @Param [authentication]
     * @return com.moyun.websocket.po.User
     */
    @RequestMapping(value="/currentuser",method = RequestMethod.GET)
    @ResponseBody
    public User currentuser(Authentication authentication){
        SysUserEntity principal = (SysUserEntity) authentication.getPrincipal();
        String name = authentication.getName();
        return new User(principal.getId(), name);
    }

    /**
     * @Author yangboxuan
     * @Description 获取当前在线用户集合
     * @Date 2021/9/5 11:37
     * @Param [currentuser]
     * @return java.util.Set<java.lang.String>
     */
    @ApiOperation("获取当前在线人数")
    @GetMapping("/onlineusers")
    @ResponseBody
    public Set<String> onlineusers(@RequestParam("currentuser") String currentuser) {
        // SessionRegistry中 两个字段属性的模型 例如 private final Map<String, SessionInformation> sessionIds;
//        ConcurrentHashMap<String, Session> map = WebSocketServer.getSessionPools();
//        Set<String> set = map.keySet();
//        Iterator<String> it = set.iterator();
        Set<String> nameset = new HashSet<String>();
        List<Object> principals = this.memorySessionUtil.getAllPrincipalsFromSessionRegistry();
        for (Object principal : principals) {
            SysUserEntity sysUserEntity = (SysUserEntity) principal;
            String username = sysUserEntity.getUsername();
            if (!username.equals(currentuser)) {
                nameset.add(username);
            }
        }
//        while (it.hasNext()) {
//            String entry = it.next();
//            if (!entry.equals(currentuser)) {
//                nameset.add(entry);
//            }
//        }
        return nameset;
    }
}
